var page_loaded = false;

bt.module({builder: function(bt){
    var j$ = jQuery;

    var sns_type_name_mapping = {
        "sina": "新浪微博"
        //"tencent": "腾讯微博",
        //"renren": "人人网"
    };

    var markWaypoint = function(point) {
        var items = point.children();
        var lat = parseFloat($(items[0]).text());
        var lon = parseFloat($(items[1]).text());

        var wp_left = 580 / 360 * (180 + lon);
        var wp_top = 340 / 180 * (90 - lat) + 25;
        var div = j$("<div></div>");
        div.addClass("location-icon");
        div.css("top", wp_top);
        div.css("left", wp_left);
        j$("#summary2012-content .map").append(div);
    };


    var connectSns = function(sns_type, sns_name) {
        sns_name = sns_name || sns_type_name_mapping[sns_type];
        alert("您还未绑定" + sns_name + "，去账户设置里绑定后再来分享吧");
        return;
    };

    var checkSNSBind = function() {
        j$.getJSON('/accounts/settings/sync_sns/status/?v=2', function(data){
            sns_data = data;
            var sns_type = "sina";
            var connected = data[sns_type];
            if(!connected) {
                connectSns(sns_type);
            } else {
                doShare();
            }
        });
    };

    var doShare = function() {
        var url = j$("#user-summary-content .share-btn").data("share-url");
        var args = j$("#share-summary2012").serialize();
        $.ajax({
            type: "POST",
            url: url,
            data: args
        }).success(function() {
                alert("分享成功");
            });
    };


    $(document).ready(function() {
        page_loaded = true;
        // Added waypoint icon on world map image
        //var points = j$('#map_data').children();
        //for (var i = 0; i < points.length; i++) {
        //    var point = j$(points[i]);
        //    markWaypoint(point);
        //}

        // Init weibo share button event
        j$("#user-summary-content .share-btn").click(function() {
            checkSNSBind();
        });
    });



}});
